/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Messenj;

import Utils.MjUtils;
import java.awt.Color;
import java.awt.Component;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import net.sf.jml.MsnContact;
import net.sf.jml.MsnUserStatus;

/**
 *
 * @author edoardo
 */
public class MjCellRenderer extends JLabel implements ListCellRenderer {

    MsnContact[] mc;
    Image[] i;
    Image i_on, i_off, i_busy, i_away, i_hide;
    int image_width = 20;
    int image_heigth = 21;

    public MjCellRenderer(MsnContact[] mc) {
        this.mc = mc;
        this.loadDefaultImg();
    }

    private void loadDefaultImg() {
        i_on = Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("Resources/Images/status/user_online.png"));
        i_off = Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("Resources/Images/status/user_offline.png"));
        i_busy = Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("Resources/Images/status/user_busy.png"));
        i_away = Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("Resources/Images/status/user_away.png"));
        i_hide = Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("Resources/Images/status/user_hide.png"));
        i_on = i_on.getScaledInstance(this.image_width, this.image_heigth, Image.SCALE_FAST);
        i_off = i_off.getScaledInstance(this.image_width, this.image_heigth, Image.SCALE_FAST);
        i_busy = i_busy.getScaledInstance(this.image_width, this.image_heigth, Image.SCALE_FAST);
        i_away = i_away.getScaledInstance(this.image_width, this.image_heigth, Image.SCALE_FAST);
        i_hide = i_hide.getScaledInstance(this.image_width, this.image_heigth, Image.SCALE_FAST);
    }

    public void reload() {
    }

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        if (mc[index].getStatus().equals(MsnUserStatus.ONLINE)) {
            this.setIcon(new ImageIcon(i_on));
        } else {
            if (mc[index].getStatus().equals(MsnUserStatus.AWAY)) {
                this.setIcon(new ImageIcon(this.i_away));
            } else {
                if (mc[index].getStatus().equals(MsnUserStatus.OFFLINE)) {
                    this.setIcon(new ImageIcon(this.i_off));
                } else {
                    if (mc[index].getStatus().equals(MsnUserStatus.HIDE)) {
                        this.setIcon(new ImageIcon(this.i_hide));
                    }
                }
            }
        }
        if (!isSelected)
        {
            this.setBackground(list.getSelectionBackground());
            this.setForeground(list.getSelectionForeground());
        }
        this.setText(mc[index].getEmail().getEmailAddress());
        this.setToolTipText(mc[index].getEmail().getEmailAddress() + " " + MjUtils.getDisplayableStatus(mc[index].getStatus()));
        return this;
    }
}
